
package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.SpecialiteDaoIn;
import com.aerotechnic.entities.Phase;
import com.aerotechnic.entities.Specialite;

@Repository("SpecialiteDao")
public class SpecialiteDao implements SpecialiteDaoIn{

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public void save(Specialite spec) {
		// TODO Auto-generated method stub
		getCurrentSession().save(spec);
		
	}

	@Override
	public void update(Specialite spec) {
		// TODO Auto-generated method stub
		getCurrentSession().update(spec);
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		getCurrentSession().delete(findByID(id));
	}

	@Override
	public Specialite findByID(Long id) {
		// TODO Auto-generated method stub
		List<Specialite> tempList=getCurrentSession().createQuery("from Specialite where id = "+id).list();
		return  tempList.isEmpty()?null: tempList.get(0);
	}

	@Override
	public List<Specialite> findAll() {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Specialite").list();
	}

	@Override
	public List<Specialite> find(int jtStartIndex, int jtPageSize) {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Specialite")
				.setFirstResult(jtStartIndex).setMaxResults(jtPageSize).list();
	}

	@Override
	public int countAllSpecialite() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				List allSpecialite = getCurrentSession().createQuery(
						"from Specialite ").list();
				return allSpecialite == null ? 0 : allSpecialite.size(); 
	}

	@Override
	public Specialite findByIntitule(String specInt) {
		// TODO Auto-generated method stub
		return (Specialite) getCurrentSession().createQuery(
				"from Specialite where intitule = '"+specInt+"'").list().get(0);
	}

}
